package com.naiterui.ehp.bs.doctor.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.consult.ComplaintBO;
import com.naiterui.ehp.bp.bo.consult.ComplaintDetailBO;
import com.naiterui.ehp.bp.domain.DrConsultComplaint;
import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.repository.IDrConsultComplaintRepository;
import com.naiterui.ehp.bs.doctor.service.IConsultComplaintService;
import com.naiterui.ehp.bs.doctor.service.IPatientService;

/**
 * @Description 会话投诉
 * @Author gongbaoqiang
 * @Date 2020/7/1 4:15 PM
 */
@Service
public class AdConsultCompalintServiceImpl implements IConsultComplaintService {

    @Autowired
    private IDrConsultComplaintRepository complaintRepository;

    @Autowired
    private IPatientService patientService;

    @Override
    public PageVO<ComplaintDetailBO> findByDoctorId(Long doctorId, PageParamVO pageParamVO) throws BusinessException {
        Page<DrConsultComplaint> page = complaintRepository.findByDoctorIdOrderByIdDesc(doctorId, pageParamVO.getPageable());
        Map<Long, Patient> patientMap = patientService.getPatientMap(page.getContent().stream()
                .map(DrConsultComplaint::getPatientId).collect(Collectors.toSet()));
        List<ComplaintDetailBO> result = page.getContent().stream().map(s ->
                ComplaintDetailBO.builder()
                    .complaintId(s.getId())
                    .phone(s.getPhone())
                    .question(s.getQuestion())
                    .patientHeadUrl(patientMap.get(s.getPatientId()).getHeadUrl())
                    .patientNickName(patientMap.get(s.getPatientId()).getName())
                    .createdAt(new Date())
                    .build()
            ).collect(Collectors.toList());

        return PageVO.newBuilder()
                .pageSize(page.getSize())
                .pageNo(page.getNumber())
                .totalCount((int)page.getTotalElements())
                .result(result)
                .build();
    }

    @Override
    public void submit(ComplaintBO complaintBO) throws BusinessException {
        if (existByConsultSession(complaintBO.getSessionId(), complaintBO.getType())) {
            throw new BusinessException(ExceptionCodes.CONSULT_COMMENT_IS_EXIST);
        }
        DrConsultComplaint complaint = DrConsultComplaint.builder()
                .doctorId(complaintBO.getDoctorId())
                .patientId(complaintBO.getPatientId())
                .question(complaintBO.getQuestion())
                .phone(complaintBO.getPhone())
                .consultSessionId(complaintBO.getSessionId())
                .consultType(complaintBO.getType())
                .createdAt(new Date())
                .build();
        complaintRepository.save(complaint);

    }

    @Override
    public Boolean existByConsultSession(Long consultSessionId, Integer consultType) throws BusinessException {
        return complaintRepository.findFirstByConsultSessionIdAndConsultType(consultSessionId, consultType) == null ? false : true;
    }

    @Override
    public ComplaintDetailBO getConsultSessionComplaint(Long consultSessionId, Integer consultType) {
        DrConsultComplaint consultComplaint =
            complaintRepository.findFirstByConsultSessionIdAndConsultType(consultSessionId, consultType);
        if (consultComplaint == null) {
            return null;
        }
        ComplaintDetailBO complaintDetailBO = new ComplaintDetailBO();
        BeanUtil.copyProperties(consultComplaint, complaintDetailBO);
        return complaintDetailBO;
    }
}
